import { constant } from './index'
import { fromJS } from 'immutable'
import axios from 'axios'

const changeHomeData = (value) => {
  return {
    type: constant.SET_HOME_DATA,
    value: value,
  }
}

const addList = (value) => {
  return {
    type: constant.LOAD_MORE,
    list: fromJS(value.list),
    nextPage: fromJS(value.nextPage),
  }
}

export const getHomeData = () => {
  return async (dispatch) => {
    const { data } = await axios.get('http://localhost:5500/data/homeData.json')
    const action = changeHomeData(data)
    dispatch(action)
  }
}

export const loadMore = (pageIndex) => {
  return (dispatch) => {
    axios
      .get(
        'http://localhost:5500/data/recommondList.json?pageIndex=' + pageIndex
      )
      .then(({ data }) => {
        const action = addList({
          list: data,
          nextPage: pageIndex + 1,
        })
        dispatch(action)
      })
  }
}

const showScroolTop = (value) => {
  return {
    type: constant.SHOW_SCROLL_TOP,
    value,
  }
}

export const changeShowScrollTop = (value) => {
  return (dispath) => {
    const action = showScroolTop(value)
    dispath(action)
  }
}
